package com.example.park_2306.video

import com.example.base.mvi.BaseViewModel
import com.example.base.mvi.HttpType
import com.example.base.net.ShopApiService
import com.example.base.net.VideoApiService
import com.example.base.room.VideoDao
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class VideoViewModel @Inject constructor(val videoApiService: VideoApiService,val videoDao: VideoDao):BaseViewModel<VideoIntent>() {
    override fun handleIntent(it: VideoIntent) {
        when(it){
            is VideoIntent.video -> httpRequest(videoApiService.video(it.channelId,it.page,it.pagesize))
            VideoIntent.videoType -> httpRequest(videoApiService.videoType(),HttpType.DICT)
            is VideoIntent.agree -> httpRequest(videoApiService.agree(toBody(it.item)),HttpType.AGREE)
            is VideoIntent.unAgree -> httpRequest(videoApiService.unagree(toBody(it.item)),HttpType.UNAGREE)
            is VideoIntent.delete -> roomRequest(flow { emit(videoDao.delete(it.item)) },HttpType.DELETE)
            is VideoIntent.insert -> roomRequest(flow { emit(videoDao.insert(it.item)) },HttpType.ADD)
            is VideoIntent.queryById -> roomRequest(flow { emit(videoDao.queryById(it.id)) },HttpType.DETAIL)
            is VideoIntent.getComment -> httpRequest(videoApiService.getComment(it.datatype,it.itemid),HttpType.COMMENT)
            is VideoIntent.sendComment -> httpRequest(videoApiService.sendComment(toBody(it.sendComment)),HttpType.SENDCOMMENT)
            is VideoIntent.sendChildComment -> httpRequest(videoApiService.sendChildComment(toBody(it.reply)),HttpType.SENDCHILDCOMMENT)
        }
    }
}